package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.lala.rennigou.pojo.EasybuyUser;
import com.lala.rennigou.pojo.EasybuyUserAddress;
import com.lala.rennigou.service.EasybuyUserAddressService;
import org.apache.jasper.compiler.JspUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class EasybuyUserAddressController {
    @Autowired
    private EasybuyUserAddressService easybuyUserAddressService;

    @RequestMapping("/addDZ")
    private Object addDz(String address, String remark,int userid){
        EasybuyUserAddress e=new EasybuyUserAddress();
        e.setAddress(address);
        e.setRemark(remark);
        e.setCreatetime(new Date());
        e.setIsdefault("0");
        e.setUserid(userid);
        int i = easybuyUserAddressService.xzAddress(e);
        if (i>0)return JSONArray.toJSON(true);
        return JSONArray.toJSON(false);
    }

    @RequestMapping("/myaddress")
    public Object myaddress(int userid){
        return JSONArray.toJSON(easybuyUserAddressService.myaddress(userid));
    }

    @RequestMapping("/insertAddress")
    public Object insertAddress(EasybuyUserAddress easybuyUserAddress){
        easybuyUserAddress.setCreatetime(new Date());
        int a = easybuyUserAddressService.insertAddress(easybuyUserAddress);
        if(a>0){
            return JSONArray.toJSON("true");
        }
        return JSONArray.toJSON("false");
    }

    @RequestMapping("/deleteAdress")
    public Object deleteAdress(int id){
        int a = easybuyUserAddressService.deleteAdress(id);
        if(a>0){
            return JSONArray.toJSON("true");
        }
        return JSONArray.toJSON("false");
    }

    @RequestMapping("/updatemoren")
    public Object updatemoren(int id,int userid){
        List<EasybuyUserAddress> list = easybuyUserAddressService.addressbyisdefault("1",userid);
        int a = easybuyUserAddressService.updatemoren(id,list,userid);
        if(a>0){
            //System.out.println(true);
            return JSONArray.toJSON("true");
        }
        //System.out.println(false);
        return JSONArray.toJSON("false");
    }

}
